package gw.com.android.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import java.util.Timer;
import java.util.TimerTask;

import gw.com.android.app.ActivityManager;
import gw.com.android.app.AppContances;
import gw.com.android.app.AppMain;
import gw.com.android.app.GTConfig;
import gw.com.android.model.ConfigSettingDeal;
import gw.com.android.model.ConfigType;
import gw.com.android.model.ConfigUtil;
import gw.com.android.model.DataManager;
import gw.com.android.presenter.LoginPresenter;
import gw.com.android.terminal.AppTerminal;
import gw.com.android.terminal.GTSDataListener;
import gw.com.android.ui.BaseActivity;
import gw.com.android.ui.MainActivity;
import gw.com.android.upgrade.AppUpgradeService;
import gw.com.android.utils.ENV;
import gw.com.android.utils.PackStatis;
import www.com.library.app.AppActivities;
import www.com.library.app.ObjectSessionStore;
import www.com.library.util.DeviceUtil;
import www.com.library.view.BtnClickListener;

/**
 * 创建一般的提示弹层的对象,带确定和取消按钮的提示弹层,可根据需要修改按钮文字和点击事件
 * @author Reeta.zou
 *
 */
public class PopupDoubleBtnDialog extends BaseDialog {

	public PopupDoubleBtnDialog(Context context) {
		super(context);
	}

	private String mTitleText;
	private String mContentText;
	private int contentGravity = Gravity.CENTER;

	/**
	 * 
	 * @param act
	 * @param contentID
	 * @param mBtnClickListener
	 * @return
	 */
	public static PopupDoubleBtnDialog newInstance(Activity act,int contentID,BtnClickListener mBtnClickListener) {
		String content = "";  
	    if (0 != contentID) {
	    	content = AppMain.getAppString(contentID);
		}
	    return newInstance(act, content, mBtnClickListener);
	}
	
	/**
	 * 
	 * @param act
	 * @param content
	 * @param mBtnClickListener
	 * @return
	 */
	public static PopupDoubleBtnDialog newInstance(Activity act,String content,BtnClickListener mBtnClickListener) {
		return newInstance(act, "", content, "", "",  mBtnClickListener);
	}
	
	/**
	 * 设置弹层的点击事件，按钮文字为“确定”
	 * @param act
	 * @param content
	 * @param title
	 * @param mBtnClickListener
	 * @return
	 */
	public static PopupDoubleBtnDialog newInstance(Activity act,String title, String content,BtnClickListener mBtnClickListener ) {
		return newInstance(act, title, content, "", "",mBtnClickListener);
	}
	
	
	/**
	 * 设置弹层的点击事件，按钮文字为“确定”
	 * @param act
	 * @param mBtnClickListener
	 * @return
	 */
	public static PopupDoubleBtnDialog newInstance(Activity act,String title, String content,String posTitle, int contentGravity ,BtnClickListener mBtnClickListener ) {
		return newInstance(act, title, content, posTitle, "", mBtnClickListener);
	}
	
	/**
	 * 设置弹层的点击事件
	 * @param act
	 * @param contentID
	 * @param mBtnClickListener
	 * @return
	 */
	public static PopupDoubleBtnDialog newInstance(Activity act,int contentID,int posID,int negID,BtnClickListener mBtnClickListener) {
	    String content = "";  
		String posTitle = "";
	    String negTitle = "";
	    if (0 != contentID) {
	    	content = AppMain.getAppString(contentID);
		}
		if (0 != posID) {
			posTitle = AppMain.getAppString(posID);
		}
		if (0 != negID) {
			negTitle = AppMain.getAppString(negID);
		}
		return newInstance(act,"", content, posTitle, negTitle,  mBtnClickListener);
	}
	
	/**
	 * 设置弹层的点击事件
	 * @param act
	 * @param mBtnClickListener
	 * @return
	 */
	public static PopupDoubleBtnDialog newInstance(Activity act,String title, String content, String posTitle, String negTitle, BtnClickListener mBtnClickListener) {
		PopupDoubleBtnDialog dialog = new PopupDoubleBtnDialog(act);
		dialog.mCustomViewResId = R.layout.dialog_action_content;
		if(null != mBtnClickListener){
			dialog.setBtnClickListener(mBtnClickListener);
		}
		if(null != title && !title.equals("")){
			dialog.mTitleText = title;
		}
		if(null != content && !"".equals(content)){
			dialog.mContentText = content;
		}
		
		if(null != posTitle && !"".equals(posTitle)){
			dialog.setPositiveBtnText(posTitle, true);
		}
		
		if(null != negTitle && !"".equals(negTitle)){
			dialog.setNegativeBtnText(negTitle, true);
		}
		
		int padding = (DeviceUtil.instance().getScreenPixelsWidth(act) - (int) (280 * DeviceUtil.instance().getScreenDensity(act)))/2;
		dialog.setPadding(padding, 0, padding, 0);
		mInstance = dialog;
		return dialog;
	}

	@Override
	public void initParam() {
		mCustomViewResId = R.layout.dialog_action_content;
		mHasPosButton = true;
		mHasNegButton = true;
	}

	@Override
	public void inflaterCustomView(View mCustomView) {
		TextView contentV = (TextView) mCustomView.findViewById(R.id.dialog_content_text);
		TextView titleV = (TextView) mCustomView.findViewById(R.id.dialog_title);
		View divider = mCustomView.findViewById(R.id.divider_view);
		if (contentV != null) {
			contentV.setText(mContentText);
			contentV.setGravity(contentGravity);
		}
		if(titleV != null){
			if ( null != mTitleText && mTitleText.length() > 0 ) {
				titleV.setText(mTitleText);
				titleV.setVisibility(View.VISIBLE);
				divider.setVisibility(View.VISIBLE);
			}else{
				titleV.setVisibility(View.GONE);
				divider.setVisibility(View.GONE);
			}	
		}
		//不设置点击事件时使用默认的点击事件
		mBtnPos.setOnClickListener(this);
		mBtnNeg.setOnClickListener(this);
	}

	/**
	 * 退出应用的弹层
	 * @param act
	 * @return
	 */
	public static PopupDoubleBtnDialog getAppExit(final Activity act){
		PopupDoubleBtnDialog mDialog = PopupDoubleBtnDialog.newInstance(act, R.string.exit_dialog_content, new BtnClickListener() {

			@Override
			public void onBtnClick(int viewID) {
				if(viewID == R.id.action_btn_pos){
					AppTerminal.instance().exitApp();
					PackStatis.getToService(PackStatis.EventAction.QUIT.getValue(),PackStatis.EventCategory.MAIN.getValue(),null,null);
					new Timer().schedule(new TimerTask() {

						@Override
						public void run() {
							act.runOnUiThread(new Runnable() {

								@Override
								public void run() {
									GTSDataListener.instance().unsubscribe();
									if (AppUpgradeService.isUpgradeing) {
										AppActivities.getSingleton().finishAllActivities();
										AppActivities.getSingleton().popAllActivityExceptOne(null);
									} else {
										// by zouyuye 2013-10-25 退出应用时停止下载更新包的service
										if (AppUpgradeService.isStartUpgrade) {
											AppMain.getApp().stopService(new Intent(AppMain.getApp(), AppUpgradeService.class));
										}
										AppActivities.getSingleton().popAllActivityExceptOne(null);
										if (GTConfig.instance().hasNotify) {
											GTConfig.instance().hasNotify = false;
											Intent intent = new Intent(Intent.ACTION_MAIN);
											intent.addCategory(Intent.CATEGORY_HOME);

											intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
											act.startActivity(intent);
											System.exit(0);
										} else {
											android.os.Process.killProcess(android.os.Process.myPid());
										}
									}
								}
							});
						}
					}, AppContances.TIMER_TASK_TIME);
				}
			}
		});
		mInstance = mDialog;
		return mDialog;
	}

	/**
	 * 切换用户的弹层
	 * @param act
	 * @return
	 */
	public static PopupDoubleBtnDialog getAppChangeUser(final Activity act){
		PopupDoubleBtnDialog mDialog = PopupDoubleBtnDialog.newInstance(act, R.string.change_dialog_content, new BtnClickListener() {

			@Override
			public void onBtnClick(int viewID) {
                if(viewID == R.id.action_btn_pos){
					ActivityManager.backLogin(act, false);
                }
			}
		});
		mInstance = mDialog;
		return mDialog;
	}

	/**
	 * 退出当前账户
	 */
	public static PopupDoubleBtnDialog getAppQuit(final Activity act){
		PopupDoubleBtnDialog mDialog = PopupDoubleBtnDialog.newInstance(act, AppMain.getAppString(R.string.exit_dialog_title_quit),
				AppMain.getAppString(R.string.exit_dialog_content_quit), new BtnClickListener() {

			@Override
			public void onBtnClick(int viewID) {
                if(viewID == R.id.action_btn_pos){
					GTConfig.instance().setBooleanValue(GTConfig.PREF_AUTO_LOGIN,false);

					GTConfig.instance().setAccountType(GTConfig.ACC_TYPE_GUEST);
					GTConfig.instance().mLastAccountType = GTConfig.ACC_TYPE_GUEST;
					GTConfig.instance().isPhoneLogin = false;
//					GTConfig.instance().phoneLoginInfo ="";
					GTConfig.instance().mCurLoginPhone ="";
					GTConfig.instance().mCurName = "";
					GTConfig.instance().mUserPwd = "";

					final MainActivity mainActivity = (MainActivity) ObjectSessionStore.getObject(MainActivity.class.getSimpleName() + GTSDataListener.mainNum);
					if(null != mainActivity){
						mainActivity.removeAllFragment(false,true);
					}
					DataManager.instance().reset();
					LoginPresenter presenter = new LoginPresenter(act);
					presenter.guestLogin();
					GTConfig.instance().setJpushAliasAndTags(null,"");
					PackStatis.getToService(PackStatis.EventAction.LOGOUT.getValue(),PackStatis.EventCategory.MAIN.getValue(),null,null);
					act.finish();
					new Handler(Looper.getMainLooper()).post(new Runnable() {
						@Override
						public void run() {
							if (mainActivity != null){
								mainActivity.showMeFragment();
							}
						}
					});

                }
			}
		});
		mInstance = mDialog;
		return mDialog;
	}

	/**
	 * 清除系统缓存的弹层
	 * @param act
	 * @return
	 */
	public static PopupDoubleBtnDialog getAppClearData(final BaseActivity act){
		PopupDoubleBtnDialog mDialog = PopupDoubleBtnDialog.newInstance(act, R.string.system_cache_dialog_content, new BtnClickListener() {

			@Override
			public void onBtnClick(int viewID) {
                if(viewID == R.id.action_btn_pos){
                	AppTerminal.instance().ClearAllCacheData();
					if(act != null){
						act.showToastPopWindow(AppMain.getAppString(R.string.system_cache_success_msg));
					}
                }
			}
		});
		mInstance = mDialog;
		return mDialog;
	}

	/**
	 * 使用qq客服的确认弹层
	 * @param mActivity
	 * @return
	 */
	public static PopupDoubleBtnDialog openQQ(final Activity mActivity){
		PopupDoubleBtnDialog openQQDialog = null;
		final String qqServiceURL = GTConfig.INTENT_QQ.replace("00000000", ConfigUtil.instance().mConfigObject.optString(ConfigType.QQ_SERVICE_VALUE_TAG));
		if (GTConfig.instance().getBooleanValue(GTConfig.PREF_USER_QQ_SERVICE, true)) {
			openQQDialog = PopupDoubleBtnDialog.newInstance(mActivity, AppMain.getAppString((R.string.qq_service_dialog_msg), ENV.getAppName()), new BtnClickListener() {

				@Override
				public void onBtnClick( int btnID) {
					if(btnID == R.id.action_btn_pos){
						try {
							mActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(qqServiceURL)));
						} catch (Exception e) {
							//如客户没装QQ，则系统会打开移动版在线客服页面
							ConfigSettingDeal mDeal = new ConfigSettingDeal();
							mDeal.openQQ((FragmentActivity) mActivity, ConfigType.QQ_SOURCE_ABOUT);
						}
						GTConfig.instance().setBooleanValue(GTConfig.PREF_USER_QQ_SERVICE, false);
					}
				}
			});
			openQQDialog.show();
		}else{
			try {
				mActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(qqServiceURL)));
			} catch (Exception e) {
				//如客户没装QQ，则系统会打开移动版在线客服页面
				ConfigSettingDeal mDeal = new ConfigSettingDeal();
				mDeal.openQQ((FragmentActivity) mActivity,ConfigType.QQ_SOURCE_ABOUT);
			}
		}
		mInstance = openQQDialog;
		return openQQDialog;
	}

}
